{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exercício 2 - Caminhos Médios\n",
    "\n",
    "Computar, para os mesmos __gráficos do Exercício 1__, a média dos caminhos mais curtos.\n",
    "\n",
    "Referência: [Wikipedia](https://en.wikipedia.org/wiki/Average_path_length)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import networkx as nx\n",
    "import network_analysis_utils as nau\n",
    "\n",
    "# Funções disponíveis:\n",
    "#\n",
    "#  - nau.facebook_nx_graph(): Obtém o grafo Networkx do facebook\n",
    "#  \n",
    "#  - nau.random_nx_graph(): Obtém o grafo Networkx randômico\n",
    "#\n",
    "# Obs: o prefixo 'nx' indica o uso da Networkx, a lib de grafos utilizada nesse exercício"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> __Dica__: está permitido o uso da [função da distância de caminhos mais curtos da Networkx](http://networkx.readthedocs.io/en/networkx-1.11/reference/generated/networkx.algorithms.shortest_paths.generic.shortest_path_length.html#networkx.algorithms.shortest_paths.generic.shortest_path_length)\n",
    "\n",
    "> __Requisito__: __não__ está permitido o uso da [função da __média__ da distância dos caminhos mais curtos da Networkx](http://networkx.readthedocs.io/en/networkx-1.11/reference/generated/networkx.algorithms.shortest_paths.generic.average_shortest_path_length.html#networkx.algorithms.shortest_paths.generic.average_shortest_path_length)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import itertools\n",
    "\n",
    "def avg_shortest_paths(nx_graph):\n",
    "    \"\"\" Retorna a média (como valor escalar) dos caminhos mais curtos no grafo\"\"\"\n",
    "    nodes = nx_graph.nodes()\n",
    "    len_nodes = len(nodes)\n",
    "    summation = sum( [ nx.shortest_path_length(nx_graph, s, t)\n",
    "                      for s, t in itertools.product(nodes, nodes) \n",
    "                      if s != t] )\n",
    "    return float(summation) / (len_nodes * (len_nodes-1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "facebook_graph = nau.facebook_nx_graph()\n",
    "random_graph = nau.random_nx_grap()\n",
    "\n",
    "print '[You] facebook graph average shortest paths: %.2f ' % avg_shortest_paths( facebook_graph )\n",
    "print '[You] random graph average shortest paths: %.2f ' % avg_shortest_paths( random_graph )\n",
    "print '[Networkx] facebook graph average shortest paths: %.2f ' % nx.average_shortest_path_length( facebook_graph )\n",
    "print '[Networkx] random graph average shortest paths: %.2f ' % nx.average_shortest_path_length( random_graph )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
